<template>
	<view v-if="cacheWs.length > 0 && reward.visible">
		<Receive v-if="reward.visible" :visible="reward.visible" :data="cacheWs[0].data.content" @close="onCloseReceive" @jump="jumpage" @onDestroy="onDestoryReward"/> 
	</view>
	<view v-if="downOrBindCache.length > 0 && downOrBindVisible">
		<XiaomiVideo v-if="downOrBindVisible" :visible="downOrBindVisible" :des="downOrBindCache[0].des" @onDestroy="destoryScanOrBindModal" @jump="jumpVideoPage" @onClose="closeDownOrBind"/>
	</view>
	<GiveVip v-if="giveVipStatus" :show="giveVipStatus" @onDestroy="closeGiveVip"/>
</template>

<script setup lang="ts">
	import { ref, computed } from 'vue'
	import XiaomiVideo from "@/components/xiaomiVideo/index.vue"
	import Receive from "@/components/pointsLotteryModal/receive.vue"
	import GiveVip from "@/components/giveVip/index.vue"; // 绑定完孩子，点击了关闭，在当前页面弹出这个弹窗
	import { useVipStore} from "@/stores/useVip.js"
	import {onShow} from "@dcloudio/uni-app"
	const vipStore = useVipStore()
	const cacheWs = computed(() => vipStore.wsData.reward) // 这是之前存储的，还没弹出来，需要弹出来
	const downOrBindCache = computed(() => vipStore.wsXiaomiDownBind)
	
	const notRepeatClick = ref(true); // true: 没有重复点击 false： 重复点击
		
	// 奖励弹窗
	const reward = ref({
		visible: false,
	})
	// 扫描或者绑定过来的值
	const downOrBindVisible = ref(false); // 扫描或者绑定过来的值
	const onCloseReceive = () => {
		reward.value.visible = false;
		setTimeout(() => {
			reward.value.visible = vipStore.haveData()
		}, 500)
	}
	const isCurrentPage = (route = '') => {
		const pathArr = route.split("/")
		const pagePath = pathArr[pathArr.length - 2]
		const pages = getCurrentPages();
		const currentPage = pages[pages.length - 1];
		return currentPage.route.includes(pagePath)
	}
	const jumpage = () => {
		const href = cacheWs.value[0]?.href
		console.log("点击分享要跳转的 ", cacheWs.value[0], !isCurrentPage(href))
		if(href && !isCurrentPage(href)){ // 如果在当前页面就不跳转
			uni.navigateTo({
				url:href
			})
		}
	}
	const onDestoryReward  = () => {
		cacheWs.value[0]?.cb()
		console.log("是否以及删除了", vipStore.wsData.reward)
	}

	const destoryScanOrBindModal = () => { // 需要消费掉之前的ws id 
		downOrBindCache.value[0]?.cb()
	}
	const jumpVideoPage = () => { // 跳转到视频播放页面  
		if(notRepeatClick.value){
			notRepeatClick.value = false;
			const href = downOrBindCache.value[0]?.href
			if(href && !isCurrentPage(href)){ // 如果在当前页面就不跳转
				uni.navigateTo({
					url:href,
					complete() {
						notRepeatClick.value = true
					}
				})
			} else {
				notRepeatClick.value = true
			}
		}
		
	}
	const closeDownOrBind = () => { // 如果是绑定，那么现在只是关闭弹窗，需要弹出 GiveVip 弹窗
		if(downOrBindCache.value[0]?.type === 'bind'){
			downOrBindVisible.value = false;
			giveVipStatus.value = true;
		}else {
			downOrBindVisible.value = false;
			setTimeout(() => {
				downOrBindVisible.value = vipStore.haveMi()
			}, 500)
		}
	}
	const closeGiveVip = () => {
		giveVipStatus.value = false;
		downOrBindVisible.value = vipStore.haveMi()
	}
	// 绑定完孩子，点击了关闭，在当前页面弹出这个弹窗
	const giveVipStatus = ref(false)
	
	
	onShow(() => {
		uni.$on('updateZhuCe',function({data}){
			console.log("有注册的过来", data)
			reward.value.visible = true;
			// reward.value.ws_current = data;
		})
		
		uni.$on('updateDownOrBind', function({data}){
			console.log("扫描或者绑定过来的值", data)
			downOrBindVisible.value = true
		})
		
		if(cacheWs.value.length > 0){
			console.log("store里面有存储的值")
			reward.value.visible = true;
			// reward.value.ws_current = cacheWs.value[0];
		}
		
		if(downOrBindCache.value.length > 0){
			console.log("应该要走到这里，现在有下载或者扫描的视频", downOrBindCache.value)
			downOrBindVisible.value = true
		}
		
	})
	
	
</script>

<style>
</style>